﻿using System;
using System.Drawing;
using System.Windows.Forms;
using static System.Windows.Forms.Control;

namespace SimpleConnection_UDP_WIN.utils
{
    internal class ViewUtil
    {

        /// <summary>
        /// 设置编辑框为空
        /// </summary>
        /// <param name="textBoxes"></param>
        public static void setEditBoxEmpty(params TextBox[] textBoxes)
        {
            foreach (TextBox box in textBoxes)
            {
                box.Text = "";
            }

        }


        /// <summary>
        /// 富文本高亮
        /// </summary>
        /// <param name="richTextBox"></param>
        /// <param name="keyword"></param>
        public static void HighlightKeywords(RichTextBox richTextBox, string keyword)
        {
            int startIndex = 0;
            while (startIndex < richTextBox.TextLength)
            {
                int index = richTextBox.Find(keyword, startIndex, RichTextBoxFinds.None);
                if (index != -1)
                {
                    richTextBox.SelectionStart = index;
                    richTextBox.SelectionLength = keyword.Length;
                    richTextBox.SelectionColor = Color.Red;
                    richTextBox.SelectionFont = new Font(richTextBox.Font, FontStyle.Bold);
                    startIndex = index + keyword.Length;
                }
                else
                {
                    break;
                }
            }
        }



        /// <summary>
        /// 判断每一个 编辑框都不为空
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static bool IsFormTextBoxValid(ControlCollection cc)
        {
            bool isValid = true;

            // 遍历每个编辑框
            foreach (Control control in cc)
            {
                Console.WriteLine($"{control.Name}");

                if (control is TextBox textBox)
                {
                    Console.WriteLine($"{control.Name}:{textBox.Text}");
                    // 检查文本是否为空
                    if (string.IsNullOrEmpty(textBox.Text))
                    {
                        isValid = false;
                        // 显示错误消息
                        break;
                    }
                }
            }

            return isValid;
        }
    }
}
